package com.moinut.studentattendancemanager.controller;

import com.moinut.studentattendancemanager.controller.callback.OnGetListListener;
import com.moinut.studentattendancemanager.controller.callback.OnGetListener;
import com.moinut.studentattendancemanager.controller.callback.OnUpdateListener;
import com.moinut.studentattendancemanager.model.vo.CourseInfo;
import com.moinut.studentattendancemanager.model.vo.TeacherInfo;
import com.moinut.studentattendancemanager.model.factory.DAOFactory;
import com.moinut.studentattendancemanager.model.vo.db.Student;
import com.sun.istack.internal.NotNull;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author MOILING
 */
public class StudentController {

    public static void updateStudentEmail(@NotNull String studentId, @NotNull String newEmail, @NotNull OnUpdateListener listener) {
        if (newEmail == null) {
            return;
        }
        if (newEmail.isEmpty()) {
            listener.onError("新邮箱不得为空");
            return;
        }
        Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(newEmail);
        if (!matcher.matches()) {
            listener.onError("新邮箱格式不对");
            return;
        }
        try {
            if (DAOFactory.getIStudentDAOInstance().updateStudentEmail(studentId, newEmail)) {
                listener.onSuccess("修改资料成功");
            } else {
                listener.onError("修改资料失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getStudentById(@NotNull String studentId, @NotNull OnGetListener<Student> listener) {
        try {
            Student student = DAOFactory.getIStudentDAOInstance().getStudentById(studentId);
            if (student != null) {
                listener.onSuccess(student);
            } else {
                listener.onError("没找到该学生");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getStudentsByName(@NotNull String studentName, @NotNull OnGetListListener<Student> listener) {
        try {
            List<Student> students = DAOFactory.getIStudentDAOInstance().getStudentsByName(studentName);
            if (students.size() > 0) {
                listener.onSuccess(students);
            } else {
                listener.onError("没找到该姓名的学生");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getStudentsByClass(@NotNull String classId, @NotNull OnGetListListener<Student> listener) {
        try {
            List<Student> students = DAOFactory.getIStudentDAOInstance().getStudentsByClass(classId);
            if (students.size() > 0) {
                listener.onSuccess(students);
            } else {
                listener.onError("没找到该教学班的学生");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getStudentsByMajor(@NotNull String major, @NotNull OnGetListListener<Student> listener) {
        try {
            List<Student> students = DAOFactory.getIStudentDAOInstance().getStudentsByMajor(major);
            if (students.size() > 0) {
                listener.onSuccess(students);
            } else {
                listener.onError("没找到该专业的学生");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getAllStudents(@NotNull OnGetListListener<Student> listener) {
        try {
            List<Student> students = DAOFactory.getIStudentDAOInstance().getAllStudents();
            if (students.size() > 0) {
                listener.onSuccess(students);
            } else {
                listener.onError("没找到任何的学生");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void updateStudentInfo(@NotNull String oldId, @NotNull Student newStudent, @NotNull OnUpdateListener listener) {
        Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(newStudent.getStudentEmail());
        if (!matcher.matches()) {
            listener.onError("新邮箱格式不对");
            return;
        }
        try {
            if (DAOFactory.getIStudentDAOInstance().updateStudentInfo(oldId, newStudent)) {
                listener.onSuccess("修改资料成功");
            } else {
                listener.onError("修改资料失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getTeacherInfo(@NotNull String studentId, @NotNull OnGetListListener<TeacherInfo> listener) {
        try {
            List<TeacherInfo> teachersInfo = DAOFactory.getITeacherInfoDAOInstance().getTeachersInfoByStudentId(studentId);
            if (teachersInfo.size() > 0) {
                listener.onSuccess(teachersInfo);
            } else {
                listener.onError("没找到任何老师，你是没选课吧！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }

    public static void getCourseInfo(@NotNull String studentId, @NotNull OnGetListListener<CourseInfo> listener) {
        try {
            List<CourseInfo> coursesInfo = DAOFactory.getICourseInfoDAOInstance().getCoursesInfoByStudentId(studentId);
            if (coursesInfo.size() > 0) {
                listener.onSuccess(coursesInfo);
            } else {
                listener.onError("没有课程信息，你是没选课吧！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            listener.onError(e.getMessage());
        }
    }
}
